#include "ParserBase.h"

ParserDateTime ParserBase::parserDateTime;

ParserBase::PARSED_COMMAND ParserBase::parseUserCommand(string userCommand){
	
	PARSED_COMMAND parsedCommand;
	bool result = tryParse(userCommand, parsedCommand);
	
	if (result == true) {
		return parsedCommand;
	}

	if (nextParser != NULL) {
		return nextParser->parseUserCommand(userCommand);
	}else {
		throw ("No parser exists to parse this command!");
	}
	
}

ParserBase::ParserBase(ParserBase* _nextParser) :nextParser(_nextParser) {
}

time_duration ParserBase::parseTime(const string& timeStr) {
	return parserDateTime.parseTime(timeStr);
}

ptime ParserBase::parseDateTime(const string& timeStr) {
	ptime parsedTime = parserDateTime.parseDateTime(timeStr);
	return parsedTime;
}

ptime ParserBase::parseDate(const string& timeStr) {
	return parserDateTime.parseDate(timeStr);
}

int ParserBase::getTaskIDFromArgument(string argument) {
	int extractedID;
	stringstream strStream(argument);

	strStream >> extractedID;
	strStream >> ws;

	// ensure that we indeed have a task ID, and there are no other
	// trailing characters
	if (strStream.fail() || !strStream.eof()) {
		throw (range_error("Invalid task Index!"));
	}

	return extractedID;
}